using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class GameManager : MonoBehaviour {

    Dave m_dave;
    UiManager m_ui;

    public static int m_currentRoom;
    public static Caracteristics m_caracteristics;
    public static bool m_gameStarted;
    public static RoomManager m_currentRoomManager;

    [SerializeField]
    AudioClip m_musicChoice;

    [SerializeField]
    AudioClip m_musicFight;

    AudioSource m_source;


    public int CurrentRoom {  get { return m_currentRoom; } set { m_currentRoom = value; } }



    RoomManager GetRoomById()
    {
        RoomManager[] rooms = FindObjectsOfType<RoomManager>();

        for(int i=0; i<rooms.Length; i++)
        {
            if (rooms[i].RoomId == m_currentRoom)
                return rooms[i];
        }

        return null;
    }

    public void startFight()
    {
        m_source.clip = m_musicFight;
        m_source.loop = true;
        m_source.Play();
    }

    // Use this for initialization
    void Start ()
    {
        m_dave = FindObjectOfType<Dave>();
        m_dave.onPlayerLoose.AddListener(gameOver);
        m_ui = FindObjectOfType<UiManager>();
        m_source = GetComponent<AudioSource>();

        m_source.clip = m_musicChoice;
        m_source.loop = true;
        m_source.Play();

        if(m_currentRoom != 0)
        {
            RoomManager r = GetRoomById();
            m_dave.transform.position = r == null ? m_dave.transform.position : r.RespawnPoint.transform.position;
        }

        if (m_caracteristics != null)
            m_dave.initDave(m_caracteristics);
        else
            m_ui.ActiveCaracChoices();


	}

    private void gameOver()
    {
        m_ui.GameOver();
    }

    // Update is called once per frame
    void Update () {
		
	}
}
